const service = require("../server/index");

const getLastBaseData = async (id_app,start, end) => {
  start = formatDate(
    new Date(start) - 7 * 24 * 3600 * 1000,
    "yyyy-MM-dd"
  );
  end = formatDate(
    new Date(end) - 7 * 24 * 3600 * 1000,
    "yyyy-MM-dd"
  );
  console.log(start)
  console.log(end)
  let res = await service.get(
    "https://tongji-api.dcloud.net.cn/uni/stat/api/page/visit-overview",
    {
      params: {
        id_app,
        p: 0,
        ch: 0,
        v: 0,
        start,
        end,
      },
    }
  );
  return res;
};

function formatDate(value, fmt, emptyText) {
  fmt = fmt || "yyyy-MM-dd hh:mm:ss";
  if (!value) return emptyText || "";
  let getDate = new Date(value);
  let o = {
    "M+": getDate.getMonth() + 1,
    "d+": getDate.getDate(),
    "h+": getDate.getHours(),
    "m+": getDate.getMinutes(),
    "s+": getDate.getSeconds(),
    "q+": Math.floor((getDate.getMonth() + 3) / 3),
    S: getDate.getMilliseconds(),
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (getDate.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  for (let k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1
          ? o[k]
          : ("00" + o[k]).substr(("" + o[k]).length)
      );
    }
  }
  return fmt;
}

module.exports = getLastBaseData;
